package com.huawei.appmarket.service.deamon.download;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appmarket.framework.activity.DownloadPauseDialog;
import o.bkz;
import o.cdb;
import o.cdg;
import o.Logger;
import o.dno;
import o.dno.c;
import o.ContentManager;
import o.dvr;
import o.dvt;
import o.dvz;
import o.ll;

public final class NetworkStateChangeHandler extends Handler {
    private static NetworkStateChangeHandler ॱ = new NetworkStateChangeHandler();
    private int ˏ = -1;

    static /* synthetic */ class AnonymousClass5 {
        static final /* synthetic */ int[] ˊ = new int[c.values().length];

        static {
            try {
                ˊ[c.ˋ.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                ˊ[c.ˊ.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
        }
    }

    private NetworkStateChangeHandler() {
    }

    public static NetworkStateChangeHandler ॱ() {
        return ॱ;
    }

    public void ˊ() {
        dno.ॱ().ˋ(this, 200);
    }

    public void handleMessage(Message message) {
        if (message.what == 200) {
            ˊ(message);
        }
    }

    private void ˊ(Message message) {
        switch (AnonymousClass5.ˊ[c.ॱ(message.arg1).ordinal()]) {
            case 1:
                if (message.obj != null) {
                    ˏ();
                    for (SessionDownloadTask sessionDownloadTask : dvt.ˏ().ͺ()) {
                        if (sessionDownloadTask != null && sessionDownloadTask.ˈ() == 6) {
                            if (sessionDownloadTask.ᐝॱ() == 0 || 2 == sessionDownloadTask.ᐝॱ()) {
                                ॱ(sessionDownloadTask);
                            }
                        }
                    }
                    dvt.ˏ().ʼ();
                    return;
                }
                return;
            case 2:
                if (dvt.ˏ().ˋ(2) > 0) {
                    Logger.ˎ("NetChangeHandler", "Pause task success in disconnect");
                    return;
                }
                return;
            default:
                return;
        }
    }

    private void ॱ(@NonNull SessionDownloadTask sessionDownloadTask) {
        String ˊ = bkz.ˊ(sessionDownloadTask.ˊˊ());
        int ॱᐝ = sessionDownloadTask.ॱᐝ();
        PackageInfo ᐝ = ((cdg) cdb.ॱ(cdg.class)).ᐝ(ˊ);
        if (ᐝ == null) {
            return;
        }
        if (sessionDownloadTask.ˌ() == 9 || sessionDownloadTask.ˌ() == 10) {
            if (ᐝ.versionCode != ॱᐝ) {
                dvt.ˏ().ॱ(sessionDownloadTask.ʾ());
            }
        } else if (ॱᐝ <= ᐝ.versionCode) {
            dvt.ˏ().ॱ(sessionDownloadTask.ʾ());
        }
    }

    private int ˎ() {
        int i = 0;
        for (SessionDownloadTask sessionDownloadTask : dvt.ˏ().ͺ()) {
            int i2;
            if (sessionDownloadTask == null || !dvt.ˏ().ˎ(sessionDownloadTask, false) || sessionDownloadTask.ᐝॱ() != 2) {
                i2 = i;
            } else if ((sessionDownloadTask.ˌ() != 8 && sessionDownloadTask.ˌ() != 9 && sessionDownloadTask.ˌ() != 10) || !sessionDownloadTask.ˍ()) {
                i2 = i + 1;
            }
            i = i2;
        }
        return i;
    }

    private void ˏ() {
        Context ˋ = ContentManager.getInstant().getContext();
        if (this.ˏ == -1) {
            this.ˏ = dvt.ॱ(ˋ);
        }
        int ॱ = dvt.ॱ(ˋ);
        if (ॱ != this.ˏ) {
            Logger.ˎ("NetChangeHandler", "lastNetworkType=" + this.ˏ + ",newNetworkType=" + ॱ);
            this.ˏ = ॱ;
            if (dvt.ˏ().ˋ(2) > 0) {
                Logger.ˎ("NetChangeHandler", "Pause task success in connect");
            }
            if (dvr.ˏ(ˋ, true)) {
                Logger.ˎ("NetChangeHandler", "Can be downloaded directly, need not to show pause dialog");
                Intent intent = new Intent();
                intent.setAction(DownloadPauseDialog.DIALOG_POPUP_DOWNBROADCAST);
                ll.ˏ(ˋ).ˋ(intent);
            } else {
                ॱ = ˎ();
                if (ॱ == 0) {
                    Logger.ˎ("NetChangeHandler", "pending num is 0");
                } else {
                    Intent intent2 = new Intent();
                    intent2.setAction(DownloadPauseDialog.DIALOG_POPUP_DOWNBROADCAST);
                    ll.ˏ(ˋ).ˋ(intent2);
                    intent2 = new Intent();
                    String str = DownloadPauseDialog.DIALOG_POPUP_BROADCAST;
                    intent2.putExtra(DownloadPauseDialog.PENDING_NUMBER, ॱ);
                    intent2.setAction(str);
                    ll.ˏ(ˋ).ˋ(intent2);
                    Logger.ˎ("NetChangeHandler", "sendNotify show dialog");
                }
            }
            dvz.ˏ().ˋ();
        }
    }
}
